package com.hy.mydesktop.client.component.factory;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.ButtonScale;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.ButtonModelEnum;
import com.hy.mydesktop.client.component.meta.ShortcutModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtAppEventType;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.MetaDataConstants;




/**
 * ButtonFactory用于创建按钮，并且根据GxtComponentMetaModel的信息，
 * 对按钮进行个性化配置
 * 
 * @author Administrator
 *
 */
public class ButtonFactory {
	public static Button createButton(GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//判断是哪一类的按钮
		//每一个的按钮如何实现？decorate？
		//根据数据库的buttton的配置信息，如何实现decorate，decorate时有无先后顺序
		return createBaseButton(gxtComponentMetaNodeModel);
		
		
	}
	
	/**
	 * 依据GxtComponentMetaModel对按钮，进行个性化配置
	 * @param gxtComponentMetaModel
	 * @return
	 */
	private static Button createBaseButton(GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		final Button button = createDefaultButton();
		AbstractComponetFactory.configureAbstractComponet(button, gxtComponentMetaNodeModel);

		//进行个性化配置
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(ButtonModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			button.setText((String)gxtComponentMetaNodeModel.get(ButtonModelEnum.TEXT.name().toLowerCase()));
			
		}if (gxtComponentMetaNodeModel.get(ButtonModelEnum.HEIGHT.name().toLowerCase())!=null) {
			
			button.setHeight((Integer)gxtComponentMetaNodeModel.get(ButtonModelEnum.HEIGHT.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
			
			final Map<String,Object> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            
		            case 302://Integer.valueOf("0003")
		            	SelectionListener<ButtonEvent> buttonSelectionListener = new SelectionListener<ButtonEvent>() {
		            		@Override   
		            	      public void componentSelected(ButtonEvent ce) {  
		            			final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {   
		            			      public void handleEvent(MessageBoxEvent ce) {   
		            			        Button btn = ce.getButtonClicked();   
		            			        Info.display("MessageBox", "The '{0}' button was pressed", btn.getText());   
		            			      }   
		            			    };   
		            			MessageBox.alert("Alert", "Access Denied", l); 
		            			Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
						    	  EventStructureMetaModel eventstructuremetamodel = 
										 (EventStructureMetaModel)data.get("eventstructuremetamodel");
						    	  System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
						    			  +eventstructuremetamodel.getControllerId()
						    			  +eventstructuremetamodel.getResponseModeId());
						    	  Dispatcher.forwardEvent(AppEvents.FORM_BUTTON_SUBMINT_ONCLICK,data);
						    	  //TODO 互斥事件
						    	  /**
						    	   * 通过一个途径，获得一个boolean标记，判断到底触发哪个事件
						    	   */
		            	      }};
		            	
					   button.addSelectionListener(buttonSelectionListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		           
		            case 10://Integer.valueOf("0003")
		            	/**
		            	 * BUTTON_ONCLICK_NEW或者，子程序初始化事件
		            	 * 然后窗口接受，并响应这个事件
		            	 * 根据得到的gxtComponentMetaModel，
		            	 * 加载树状控制器
		            	 * 然后，再向这树状控制器，发生事件（？），
		            	 * 初始化控制器对应的窗口或程序
		            	 */
		            	/**
		            	 * BUTTON_ONCLICK_NEW或者，子程序初始化事件，应该包含什么信息
		            	 * 1、哪个控制器响应这个事件
		            	 * 2
		            	 * gxtComponentMetaModel包含
		            	 */
		            	/**
		            	 * 如何初始化子程序
		            	 * 1、通过Rpc，加载控制器
		            	 * 2、Dispatcher.get().addController，添加此控制器
		            	 * 3、初始化，此控制器对应的窗口
		            	 * 4、在父窗口中，添加此子窗口 window.setData("children",List<Window>);
		            	 */
		            	SelectionListener<ButtonEvent> buttonSelectionListener10 = new SelectionListener<ButtonEvent>() {
		            		@Override   
		            	      public void componentSelected(ButtonEvent ce) {  
		            			
		            				//if (button.getData("0100")==null||!(Boolean)button.getData("0100")) {
										
		            					Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
				            			data.put("gxtToSeamServiceParameter", button.getData("gxtToSeamServiceParameter"));
								    	EventStructureMetaModel eventstructuremetamodel = 
												 (EventStructureMetaModel)data.get("eventstructuremetamodel");
								    	System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
								    			  +eventstructuremetamodel.getControllerId()
								    			  +eventstructuremetamodel.getResponseModeId());
								    	//button.setData("0010",true);
								    	Dispatcher.forwardEvent(AppEvents.DESKTOP_SUB_APPLICATION_INIT,data);
									//}
						    	  
		            	      }};
		            	
					   button.addSelectionListener(buttonSelectionListener10);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		           
	           		           
		            default:
		                break;
		        }
			}
		}
		return button;
		/*//gxtComponentMetaModel.get(property, valueWhenNull);
		
		//反射，解析field，查看gxtComponentMetaModel中是否存在当前属性，如果存在，则switch|case赋值
		Class c = Class.forName("com.test.client.component.meta.ButtonModel");
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
		//	Map<String, Object> buttonAttribute = new HashMap<String, Object>()
			gxtComponentMetaModel.get(fields[i].getName(), null);
		}
		for (ButtonModelEnum buttonModelEnum:ButtonModelEnum.values()) {
			//读取gxtComponentMetaModel中Button的每一个属性
			
			Map<ButtonModelEnum, Object> buttonAttribute = new HashMap<ButtonModelEnum, Object>();
			buttonAttribute.put(buttonModelEnum, 
					gxtComponentMetaModel.get(buttonModelEnum.name().toLowerCase(), null));
		}
		Enum..valueOf(enumType, name)
		switch (key) {
		case ButtonAttributeEnum.TITLE:
			gxtComponentMetaModel.get(ButtonAttributeEnum.TITLE., valueWhenNull)
			//break;  没有break

		default:
			break;
		}*/
		
	}
	
	/**
	 * DefaultButton的基本UI效果
	 * 按钮显示的文字是“确定”
	 * 按钮中等大小
	 * @return
	 */
	private static Button createDefaultButton() {
		
		Button button = new Button("确定");
		button.setText("text");
		//button.setScale(ButtonScale.MEDIUM);
		//button.setHeight(50);
		return button;
	}
	/**
	 * 依据GxtComponentMetaModel对按钮，进行个性化配置
	 * @param gxtComponentMetaModel
	 * @return
	 */
	/**
	private Button configureButton(Button button,String decoratedAttribute) {
		//遍历gxtComponentMetaModel中可能存在的属性，并赋值
		switch (1) {
		case ButtonModelEnum.TITLE:
			if (gxtComponentMetaModel.get("title")!=null) {
				
			}
			button
			break;

		default:
			break;
		}
//		if (gxtComponentMetaModel.get("title")!=null) {
		//逐一取出ButtonModelEnum中属性值与gxtComponentMetaModel中对应的属性值对比
		if (gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase())!=null) {
			
		}else if (gxtComponentMetaModel.get("title")!=null) {
			
		}else if (gxtComponentMetaModel.get("title")!=null) {
			
		}else if (gxtComponentMetaModel.get("title")!=null) {
			
		}else if (gxtComponentMetaModel.get("title")!=null) {
			
		} else {

		}
	}
	*/

	public static Button createItemOfAssociateToWindow(
			ItemOfAssociateToWindow gxtComponentMetaNodeModel) {
		final Button button = createDefaultButton();
		//AbstractComponetFactory.configureAbstractComponet(button, gxtComponentMetaNodeModel);

		//进行个性化配置
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(ButtonModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			button.setText((String)gxtComponentMetaNodeModel.get(ButtonModelEnum.TEXT.name().toLowerCase()));
			
		}
		if(gxtComponentMetaNodeModel.getRegisterEventType()!=null){
			GxtAppEventType gxtAppEventType = gxtComponentMetaNodeModel.getRegisterEventType();
			//final Map<String,Object> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			List<String> list = new ArrayList<String>();
			list.add(gxtAppEventType.getRegisterEventType());
//			EventStructureMetaModel eventstructuremetamodel = (EventStructureMetaModel)data.get("eventstructuremetamodel");
			final EventStructureMetaModel eventstructuremetamodel =gxtComponentMetaNodeModel.getEventStructureMetaModel();
//			for ( final String code : appEventCodes.keySet()) {
			for ( final String code : list) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            
		            case 302://Integer.valueOf("0003")
		            	SelectionListener<ButtonEvent> buttonSelectionListener = new SelectionListener<ButtonEvent>() {
		            		@Override   
		            	      public void componentSelected(ButtonEvent ce) {  
		            			final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {   
		            			      public void handleEvent(MessageBoxEvent ce) {   
		            			        Button btn = ce.getButtonClicked();   
		            			        Info.display("MessageBox", "The '{0}' button was pressed", btn.getText());   
		            			      }   
		            			    };   
		            			MessageBox.alert("Alert", "Access Denied", l); 
		            			//Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
		            			Map<String, Object> data = new HashMap<String, Object>();
						    	//EventStructureMetaModel eventstructuremetamodel = (EventStructureMetaModel)data.get("eventstructuremetamodel");
		            			data.put(MetaDataConstants.EVENT_STRUCTURE_METAMODEL, eventstructuremetamodel);
		            			System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
						    			  +eventstructuremetamodel.getControllerId()
						    			  +eventstructuremetamodel.getResponseModeId());
						    	  Dispatcher.forwardEvent(AppEvents.FORM_BUTTON_SUBMINT_ONCLICK,data);
						    	  //TODO 互斥事件
						    	  /**
						    	   * 通过一个途径，获得一个boolean标记，判断到底触发哪个事件
						    	   */
		            	      }};
		            	
					   button.addSelectionListener(buttonSelectionListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		           
		            case 10://Integer.valueOf("0003")
		            	/**
		            	 * BUTTON_ONCLICK_NEW或者，子程序初始化事件
		            	 * 然后窗口接受，并响应这个事件
		            	 * 根据得到的gxtComponentMetaModel，
		            	 * 加载树状控制器
		            	 * 然后，再向这树状控制器，发生事件（？），
		            	 * 初始化控制器对应的窗口或程序
		            	 */
		            	/**
		            	 * BUTTON_ONCLICK_NEW或者，子程序初始化事件，应该包含什么信息
		            	 * 1、哪个控制器响应这个事件
		            	 * 2
		            	 * gxtComponentMetaModel包含
		            	 */
		            	/**
		            	 * 如何初始化子程序
		            	 * 1、通过Rpc，加载控制器
		            	 * 2、Dispatcher.get().addController，添加此控制器
		            	 * 3、初始化，此控制器对应的窗口
		            	 * 4、在父窗口中，添加此子窗口 window.setData("children",List<Window>);
		            	 */
		            	SelectionListener<ButtonEvent> buttonSelectionListener10 = new SelectionListener<ButtonEvent>() {
		            		@Override   
		            	      public void componentSelected(ButtonEvent ce) {  
		            			
		            				//if (button.getData("0100")==null||!(Boolean)button.getData("0100")) {
										
		            			Map<String, Object> data = new HashMap<String, Object>();
				            			data.put("gxtToSeamServiceParameter", button.getData("gxtToSeamServiceParameter"));
								    	EventStructureMetaModel eventstructuremetamodel = 
												 (EventStructureMetaModel)data.get("eventstructuremetamodel");
								    	System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
								    			  +eventstructuremetamodel.getControllerId()
								    			  +eventstructuremetamodel.getResponseModeId());
								    	//button.setData("0010",true);
								    	Dispatcher.forwardEvent(AppEvents.DESKTOP_SUB_APPLICATION_INIT,data);
									//}
						    	  
		            	      }};
		            	
					   button.addSelectionListener(buttonSelectionListener10);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		           
	           		           
		            default:
		                break;
		        }
			}
		}
		return button;
		/*//gxtComponentMetaModel.get(property, valueWhenNull);
		
		//反射，解析field，查看gxtComponentMetaModel中是否存在当前属性，如果存在，则switch|case赋值
		
		for (ButtonModelEnum buttonModelEnum:ButtonModelEnum.values()) {
			//读取gxtComponentMetaModel中Button的每一个属性
			
			Map<ButtonModelEnum, Object> buttonAttribute = new HashMap<ButtonModelEnum, Object>();
			buttonAttribute.put(buttonModelEnum, 
					gxtComponentMetaModel.get(buttonModelEnum.name().toLowerCase(), null));
		}
		
		}*/
	}


}
